﻿// <copyright file="RelayCommand.cs" company="Oleg V. Polikarpotchkin">
// Copyright Josh Smith.
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-07-26</date>
// <summary>OpenWPFChart library. RelayCommand.</summary>
// <revision>$Id$</revision>

using System;
using System.Diagnostics;
using System.Windows.Input;

namespace OpenWPFChart.Helpers
{
	/// <summary>
	/// RelayCommand by Josh Smith.
	/// </summary>
	public class RelayCommand : ICommand
	{
		readonly Action<object> _execute;
		readonly Predicate<object> _canExecute;        

		public RelayCommand(Action<object> execute)
			: this(execute, null)
		{
		}

		public RelayCommand(Action<object> execute, Predicate<object> canExecute)
		{
			if (execute == null)
				throw new ArgumentNullException("execute");

			_execute = execute;
			_canExecute = canExecute;           
		}

		#region ICommand Members
		[DebuggerStepThrough]
		public bool CanExecute(object parameter)
		{
			return _canExecute == null ? true : _canExecute(parameter);
		}

		public event EventHandler CanExecuteChanged
		{
			add { CommandManager.RequerySuggested += value; }
			remove { CommandManager.RequerySuggested -= value; }
		}

		public void Execute(object parameter)
		{
			_execute(parameter);
		}
		#endregion ICommand Members
	}
}
